package table2;
//Description:双向链表
//带傀儡节点的双向链表
class ListNode{
    public int val;
    public ListNode prev;//前驱节点
    public ListNode next;//后继节点
    ListNode(int val){
        this.val = val;
    }
}
public class MyDoubleLinkedList {
    //public ListNode head;//指向双线链表的头
    ListNode head = new ListNode(-1);
    public ListNode last;//指向双向链表的尾巴

    //打印
    public void display() {
        ListNode cur = this.head.next;
        while (cur != null) {
            System.out.print(cur.val + " ");
            cur = cur.next;
        }
        System.out.println();
    }

    //长度
    public int size() {
        if (this.head.next == null) return 0;
        ListNode cur = this.head.next;
        int count = 0;
        while (cur != null) {
            count++;
            cur = cur.next;
        }
        return count;
    }

    //是否包含关键字key
    public boolean contains(int key) {
        if (this.head.next == null) return false;
        ListNode cur = this.head.next;
        while (cur != null) {
            if (cur.val != key) {
                cur = cur.next;
            } else {
                return true;
            }
        }
        return false;
    }

    //头插
    public void addFirst(int data) {
        ListNode cur = new ListNode(data);
        if (this.head.next == null) {//第一次插
            this.head.next = cur;
            this.last = cur;
        } else {
            this.head.next.prev = cur;
            cur.next = this.head.next;
            this.head.next = cur;
        }
    }

    //尾插
    public void addLast(int data) {
        ListNode cur = new ListNode(data);
        if (this.head.next == null) {//第一次插
            this.head.next = cur;
            this.last = cur;
        } else {
            this.last.next = cur;
            cur.prev = this.last;
            this.last = cur;
        }
    }

    //删除第一次出现的关键字key
    public void remove(int key) {//画图 多画图  多多画图
        ListNode cur = this.head.next;
        while (cur != null) {
            if (cur.val == key) {
                if (cur == this.head.next) {//是头节点
                    this.head.next = this.head.next.next;
                    if (this.head.next != null) {//只有一个节点
                        this.head.prev = null;
                    } else {
                        last = null;
                    }
                    //cur.next.prev = cur.prev;//要修改的地方
                } else {
                    if (cur == this.last) {//是尾巴节点
                        this.last = cur.prev;
                        cur.prev.next = cur.next;//要修改的地方
                    } else {//中间节点
                        cur.next.prev = cur.prev;
                        cur.prev.next = cur.next;
                    }
                }
                return;
            } else {
                cur = cur.next;
            }
        }

    }
    public void removeAll(int key) {
        ListNode cur = this.head.next;
        while (cur != null) {
            if (cur.val == key) {
                if (cur == this.head.next) {//是头节点
                    this.head.next = this.head.next.next;
                    //只有一个节点
                    this.head.next.prev = null;
                    }else{
                    cur.prev.next = cur.next;
                    if (cur.next == null) {//是尾巴节点
                        this.last = cur.prev;
                        //要修改的地方
                    } else {//中间节点
                            cur.next.prev = cur.prev;
                            //cur.prev.next = cur.next;
                    }
                }

            }
                cur = cur.next;

        }
    }
    //寻找index
    public ListNode searchIndex(int index){
        ListNode cur = this.head.next;
        while(index != 0){
            cur = cur.next;
            index--;
        }
        return cur;
    }
    //任意位置插入第一个数据结点为0号下标
    public void addInsert(int index,int data){
        if(index < 0 || index > size()){
            System.out.println("不合法");
            return;
        }
        ListNode node = new ListNode(data);
            if(index == 0){
                addFirst(data);
            }else if(index == size()){
                        addLast(data);
                    }else{
                ListNode cur = searchIndex(index);
                node.next = cur.prev.next;//改变四个节点
                cur.prev.next = node;
                node.prev = cur.prev;
                cur.prev = node;
            }
    }
    public void clear() {
        while(head.next != null) {
            ListNode cur = this.head.next.next;
            head.prev = null;
            head.next.next = null;
            head.next = cur;
        }
        last = null;
    }
}
